import { Effect, Reducer } from 'umi';
import { getHousesList } from '../api/index'

export interface IndexModelState {
    housesList: Array<any>;
}

export interface IndexModelType {
    state: IndexModelState;
    effects: {
        GET_HOUSES_LIST: Effect;
    };
    reducers: {
        SET_LIST: Reducer<IndexModelState>;
    };
}

const IndexModel: IndexModelType = {
    state: {
        housesList: [],
    },

    effects: {
        *GET_HOUSES_LIST({ payload }, { call, put }) {
            const { data } = yield getHousesList()
            yield put({ type: "SET_LIST", data: data.RECORDS })
        },
    },
    reducers: {
        SET_LIST(state, action) {
            const NewState = JSON.parse(JSON.stringify(state))
            NewState.housesList = action.data
            return NewState
        },
    }
};

export default IndexModel;